/**
 * Created by 静龄 on 2015/11/11.
 */

var warningaudio = document.getElementById('warningaudio');
var anime=true;
!
    function(){
        $('#myModal').modal({
            show:false
        });

        var socket;
        function init(userid){
            var host = "ws://192.168.51.183:8888/send?userid="+userid;
            try{
                socket = new WebSocket(host);
                socket.onopen    = function(msg){
                };
                socket.onmessage = function(msg){
                    var data= $.parseJSON(msg.data);
                    console.log(data);
                    if(data.type==1 ){
                        warningaudio.play();
                        var idarray=data.msg.split(",");
                        var idlength=idarray.length;
                        for(var i=0;i<idlength;i++){
                            $.post(NEWS_LIST,{id:idarray[i]},function(postdata){
                                var postdata= $.parseJSON(postdata);
                                //$.globalMessenger().post("有新的符合报警的新闻");
                                $(".warninginfo:eq(0)").children("div").show();
                                $(".warninginfo:eq(0)").children("a").children("div").html('有新的符合报警的新闻');
                            });

                        }
                        //$.globalMessenger().post("有新的符合报警的新闻");

                    }else if(data.type==2){
                        $(".warninginfo:eq(1)").children("div").show();
                        $(".warninginfo:eq(1)").children("a").children("div").html(data.msg);
                        //$.globalMessenger().post(data.msg);
                    }else{
                        $(".warninginfo:eq(2)").children("div").show();
                        $(".warninginfo:eq(2)").children("a").children("div").html(data.msg);
                    }
                };
                socket.onclose   = function(msg){
                    //$.globalMessenger().post(msg.data+"<br>链接断开，请刷新页面!");
                };
            }
            catch(ex){
                //$.globalMessenger().post(ex);
            console.log(ex);}
        }


        $._messengerDefaults = {
            extraClasses: 'messenger-fixed messenger-theme-future messenger-on-top messenger-on-right'
        };

        $.post(LOGIN_OPERATION,{"flag":"1"},function(data){
            data= $.parseJSON(data);
            if(data.code==1){
                $("#userset").attr("data-id",data.result.uid);
                $(".u_name").html(data.result.name);
                try{
                    init(data.result.uid);
                }catch(ex){
                    $.globalMessenger().post("错误"+ex);
                }
            }else{
                alert("请先登陆");
                window.location.href=__APP+"/Home/index/loginpage";
            }

        });


        if(thispageindex_temp==2){
            $.post(TASK_LIST,{"type":"1"},function(data){
                data= $.parseJSON(data);
                if(data.code==1){
                    var datalength=data.result.length;
                    for(var i=0;i<datalength;i++){
                        $(".left_answer_list").append('' +
                            '<li><a href="'+__SELF+'?id='+data.result[i].id+'">'+data.result[i].task_name+'</a></li>');
                    }
                }else{
                    if(data.result=="登录失效重新登陆"){
                        alert("请先登陆");
                        window.location.href=__APP+"/Home/index/loginpage";
                    }
                }
            });
        }else{
            $.post(TASK_LIST,{"type":"2"},function(data){
                data= $.parseJSON(data);
                if(data.code==1){
                    var datalength=data.result.length;
                    for(var i=0;i<datalength;i++){
                        $(".left_search_list").append('' +
                            '<li><a href="'+__SELF+'?id='+data.result[i].id+'">'+data.result[i].task_name+'</a></li>');
                    }
                }else{
                    if(data.result=="登录失效重新登陆"){
                        alert("请先登陆");
                        window.location.href=__APP+"/Home/index/loginpage";
                    }
                }
            });
        }


        $(".layout_logout").click(function(e){
            $.post(LOGIN_OPERATION,{"flag":"0"},function(data){
                data= $.parseJSON(data);
                if(data.code==1){
                    alert(data.result);
                    window.location.href=__APP+"/Home/index/loginpage";
                }
            });
        });

        $(".openRwindow_l").click(function(e){
            $("#userset").show();
        });

        $("#userset").on("click",".R_windows_head .R_window_close_button",function(e){
            $(this).parent().parent().hide();
        });
        $(".l_cancer").click(function(e){
            $("#userset").css("display","none");
        });
        $(".l_useredit").click(function(e){
            if(Rcheck.checknull($(".l_password input").val())){
                alert("密码不能为空");
                return false;
            }else if($(".l_password input").val()!=$(".l_rppassword input").val()){
                alert("两次输入的密码不一致");
                return false;
            }
            $.post(UPDATE_USER,{"id":$("#userset").attr("data-id"),"pwd":$(".l_password input").val(),"email":$(".l_email input").val()},function(data){
                data= $.parseJSON(data);
                if(data.code==1){
                    $("#userset").css("display","none");
                    alert(data.result);
                }else{
                    alert(data.result);
                }
            });
        });
        $('body').append("<div style='    background:rgb(219,229,237);border: 0px solid #000;padding: 4px 8px;color: black; font-size: 12px;position: absolute;z-index: 99;display: none;' class='allMsg'></div>");

        $(".showallmsg").on("mouseover","div",function(e){
            if($(this).css("overflow")=="hidden"&& $(this).hasClass("mouseover_show")){
                $(".allMsg").css('top', $(this).offset().top+23+"px");
                $(".allMsg").css('left',$(this).offset().left+"px");
                $(".allMsg").stop().fadeIn('fast');
                $(".allMsg").width($(this).width());
                $(".allMsg").html($(this).html());
            }else{

            }
        });
        $(".showallmsg").mouseout(function(e){
            $(".allMsg").stop().fadeOut('fast');
        });

        $(".warninginfo").click(function(e){
            //$(this).children("a").children("div").slideToggle("fast");
            console.log($(this).children("a").children("div").css("display"));
            if($(this).children("a").children("div").css("display")=="none" && $(this).children("a").children("div").html()!=""){




                var width=$(this).children("a").children("div").html().length*16;
                console.log(width);
                $(this).children("div").hide();
                $(this).children("a").children("div").css("width","0px");
                $(this).children("a").children("div").css("display","block");
                $(this).children("a").children("div").stop().animate({width:width+"px"},"fast");
                $(this).addClass("open");
            }else{
                var thisdom=$(this);
                $(this).children("a").children("div").stop().animate({width:"0px"},"fast",function(e){
                    thisdom.children("a").children("div").css("display","none");
                    thisdom.children("a").children("div").css("width","auto");
                    thisdom.removeClass("open");
                });
            }
        });

    }();